package com.xboard.server.dao.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.xboard.server.common.exception.DataAccessException;
import com.xboard.server.dao.MovieDao;
import com.xboard.server.manager.DataManager;

/**
 * Handles requests for the application home page.
 */
@Repository
public class MovieDaoImpl implements MovieDao{

	@Autowired
	private DataManager dataManager;
	
	// Movie data
	private String TableName = "Movie_Data";  // JDO table name
	private String Key_Movie = "MOVIE_ID"; 
	
	public MovieDaoImpl(){
		
	}
	
	@Override
	public void addMovie(Map<String, Object> Movie){
		try {
		    Key MovieKey = KeyFactory.createKey(TableName, UUID.randomUUID().toString());
	        
		    Date date = new Date();
		    Entity en = getEntity(TableName, Key_Movie, Movie.get(Key_Movie).toString());
		    if(en!=null && en.hasProperty(Key_Movie)){
		    	updateMovie(Movie);
		    }else{
		        Entity enty = new Entity(TableName, MovieKey);
		        enty.setProperty("MOVIE_ID",Movie.get("MOVIE_ID"));
		        enty.setProperty("RANK_NO",Movie.get("RANK_NO"));
		        enty.setProperty("MOVIE_NAME",Movie.get("MOVIE_NAME"));
		        enty.setProperty("OPEN_DATE",Movie.get("OPEN_DATE"));
		        enty.setProperty("TITLE_IMAGE",Movie.get("TITLE_IMAGE"));
		        enty.setProperty("MOVIE_LINK",Movie.get("MOVIE_LINK"));
		        enty.setProperty("GRADE_SCORE",Movie.get("GRADE_SCORE"));
		        enty.setProperty("SHOW_COUNT",Movie.get("SHOW_COUNT"));
		        
				enty.setProperty("date", date);
				dataManager.insert(enty);
		    }
		} catch (Exception e) {
			throw new DataAccessException(e);
		}
	}
	@Override
	public List<Map<String,Object>> listMovie(){
		List<Map<String,Object>> list= new ArrayList<Map<String,Object>>();
		Query query = new Query(TableName).addSort("RANK_NO", Query.SortDirection.ASCENDING);

		try {
			list = dataManager.list(query);

		} catch (Exception e) {
			throw new DataAccessException(e);
		}
		return list;
	}
	@Override
	public Map<String,Object> getMovie(String key){
		Map<String,Object> data = new HashMap<String,Object>();
		try {
			
			Entity enty = getEntity(TableName,Key_Movie,key);
			data.put("MOVIE_ID",enty.getProperty("MOVIE_ID"));
			data.put("RANK_NO",enty.getProperty("RANK_NO"));
			data.put("MOVIE_NAME",enty.getProperty("MOVIE_NAME"));
			data.put("OPEN_DATE",enty.getProperty("OPEN_DATE"));
			data.put("TITLE_IMAGE",enty.getProperty("TITLE_IMAGE"));
			data.put("MOVIE_LINK",enty.getProperty("MOVIE_LINK"));
			data.put("GRADE_SCORE",enty.getProperty("GRADE_SCORE"));
			data.put("SHOW_COUNT",enty.getProperty("SHOW_COUNT"));
			data.put("date", enty.getProperty("date"));
			
		} catch (Exception e) {
			throw new DataAccessException(e);
		}
		return data;
	}	
	@Override
	public void updateMovie(Map<String, Object> Movie){
		try {
			String key = Movie.get(Key_Movie).toString();
			Date date = new Date();
			
			Entity enty = getEntity(TableName,Key_Movie,key);			

			enty.setProperty("RANK_NO",Movie.get("RANK_NO"));
			enty.setProperty("MOVIE_NAME",Movie.get("MOVIE_NAME"));
			enty.setProperty("OPEN_DATE",Movie.get("OPEN_DATE"));
			enty.setProperty("TITLE_IMAGE",Movie.get("TITLE_IMAGE"));
			enty.setProperty("MOVIE_LINK",Movie.get("MOVIE_LINK"));
			enty.setProperty("GRADE_SCORE",Movie.get("GRADE_SCORE"));
			enty.setProperty("SHOW_COUNT",Movie.get("SHOW_COUNT"));
			enty.setProperty("date", date);
						
			dataManager.update(enty);	
		} catch (Exception e) {
			throw new DataAccessException(e);
		}
	}
	@Override
	public void deleteMovie(String key){
		try {
			dataManager.delete(getEntity(TableName,Key_Movie,key));
			//dataManager.delete(getEntity(TableName,"MOVIE_LINK",key)); // Link를 이용한 삭제
			
		} catch (Exception e) {
			throw new DataAccessException(e);
		}
	}
	
	@Override
	public Key getKey(String table, String name, String value){
		Key key = null;
		try {
	        Query query = new Query(table);
			query.addFilter(name, FilterOperator.EQUAL, value);
			
			key = dataManager.getKey(query);
			
		} catch (Exception e) {
			throw new DataAccessException(e);
		}
		return key;
	}		
	@Override
	public Entity getEntity(String table, String name, String value){
		Entity enFx = null;
		try {
	        Query query = new Query(table);
			query.addFilter(name, FilterOperator.EQUAL, value);
			enFx = dataManager.getEntity(query);
			
		} catch (Exception e) {
			throw new DataAccessException(e);
		}
		return enFx;
	}		
}
